const Router = require('koa-router')
const insertContent = new Router()
//！拿到前端的数据
//数据库连接池
const pg = require('pg');
let psql = new pg.Pool({
    database: 'running',
    user: 'run',
    port: 5432,
    host: '82.157.94.115',
    password: 'z12345678',
    max: 2
  })
  function makeId() {
    let id = Math.random().toString(16).substring(2);
    let id2 = Math.random().toString(16).substring(2);
    return (id + id2).substring(0, 12);
}
insertContent.post('/insertContent', async ctx => {
    // ctx.body = ''
    let {content_title,content_content,content_time} = ctx.request.body;
    console.log("content_title",content_title);
    console.log("content_content",content_content);
    console.log("content_time",content_time);
    // console.log("content_img",content_img);
    
    let insertContentPostgresql = `insert into content (content_id,content_title,content_content,content_time,content_img) values ($1,'${content_title}', '${content_content}', '${content_time}','http://82.157.94.115:2009/static/images/123.jpg')`;//, '${content_img}'
    let content = await psql.query(insertContentPostgresql, [makeId()]);

    if (content.rowCount > 0)
        ctx.body = {
            msg:'插入成功'
        };
})

module.exports = insertContent;